// pages/user/address/addressEdit/addressEdit.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        region: [],
        userName: '',
        userPhone: '',
        userAddress: '',
        isDefault: 1,
        addressId: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        let editAddress = wx.getStorageSync('editAddress')
        this.setData({
            region: [editAddress.province, editAddress.city, editAddress.district],
            userName: editAddress.consignee,
            userPhone: editAddress.mobile,
            userAddress: editAddress.address,
            isDefault: editAddress.default_id,
            addressId: editAddress.address_id
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    nameInput: function(e) {
        this.setData({
            userName: e.detail.value
        })
    },

    phoneInput: function(e) {
        this.setData({
            userPhone: e.detail.value
        })
    },

    addressInput: function(e) {
        this.setData({
            userAddress: e.detail.value
        })
    },

    setDefault: function(e) {
        this.setData({
            isDefault: e.detail.value ? 2 : 1
        })
    },

    bindRegionChange: function(e) {
        this.setData({
            region: e.detail.value
        })
    },

    goSaveAddress: function() {
        if (this.data.userName == '') {
            wx.showToast({
                title: '请填写收货人姓名',
                icon: 'none',
                duration: 1000
            })
            return
        }
        if (!(/^1\d{10}$/.test(this.data.userPhone))) {
            wx.showToast({
                title: '手机号有误',
                icon: 'none',
                duration: 1000
            })
            return
        }
        if (this.data.userRegion == '') {
            wx.showToast({
                title: '请填写收货人所在地区',
                icon: 'none',
                duration: 1000
            })
            return
        }
        if (this.data.userAddress == '') {
            wx.showToast({
                title: '请填写收货人详细地址',
                icon: 'none',
                duration: 1000
            })
            return
        }
        let data = {
            consignee: this.data.userName,
            mobile: this.data.userPhone,
            province: this.data.region[0],
            city: this.data.region[1],
            district: this.data.region[2],
            address: this.data.userAddress,
            default_id: this.data.isDefault,
            address_id: this.data.addressId
        }
        common.post('Address/address_update', data).then(function(res) {
            wx.removeStorageSync('editAddress')
            wx.showToast({
                title: '保存地址成功',
                icon: 'none',
                duration: 1000
            })
            wx.navigateBack({
                delta: 1
            })
        }).catch(function(e) {})
    },

    deleteAddress: function() {
        const that = this
        wx.showModal({
            title: '提示',
            content: '确认删除地址？',
            success(res) {
                if (res.confirm) {
                    let data = {
                        address_id: that.data.addressId
                    }
                    common.post('Address/address_delete', data).then(function(res) {
                        wx.removeStorageSync('editAddress')
                        wx.showToast({
                            title: '删除地址成功',
                            icon: 'none',
                            duration: 1000
                        })
                        wx.navigateBack({
                            delta: 1
                        })
                    }).catch(function(e) {})
                } else if (res.cancel) {
                    return
                }
            }
        })
    }
})